package com.devilloki.viki.overspeedalert;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.devilloki.viki.overspeedalert.Events.ConfigEvent;
import com.devilloki.viki.overspeedalert.fragments.DisplayAvgSpeedFragment;
import com.devilloki.viki.overspeedalert.fragments.DisplayGpsInfoFragment;
import com.devilloki.viki.overspeedalert.fragments.DisplaySpeedFragment;
import com.devilloki.viki.overspeedalert.fragments.DisplayTotalDistanceFragment;
import com.devilloki.viki.overspeedalert.fragments.DisplayTrackTimeFragment;
import com.software.shell.fab.ActionButton;

import de.greenrobot.event.EventBus;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Intent service=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("debug", "activity started");
        //重置行程按钮
        ActionButton actBtnReload
                = (ActionButton) findViewById(R.id.act_btn_reload);
        actBtnReload.setOnClickListener(this);
        //加载fragment
        if (savedInstanceState == null) {
            Log.d("debug", "activity started savedInstanceState is null");
            //防止从后台切换到前台,或者屏幕旋转时新建fragment
            FragmentManager fm = getFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();

            DisplaySpeedFragment mDisplayFragment = new DisplaySpeedFragment();
            DisplayAvgSpeedFragment mDisplayAvgSpeedFragment=new DisplayAvgSpeedFragment();
            DisplayGpsInfoFragment mDisplayGpsInfoFragment =new DisplayGpsInfoFragment();
            DisplayTotalDistanceFragment mDisplayTotalDistanceFragment =new DisplayTotalDistanceFragment();
            DisplayTrackTimeFragment mDisplayTrackTimeFragment =new DisplayTrackTimeFragment();

            transaction.replace(R.id.framUp, mDisplayFragment);
            transaction.replace(R.id.frameDownLeft, mDisplayAvgSpeedFragment);
            transaction.replace(R.id.frameDownRight,mDisplayTotalDistanceFragment);
            transaction.replace(R.id.framBottom,mDisplayGpsInfoFragment);
            transaction.replace(R.id.frameDown,mDisplayTrackTimeFragment);

            transaction.commit();
        }
        // 获取记录轨迹的service
        service=((MyApp)this.getApplication()).getService();
        //这里启动service,
        startService(service);
    }

    @Override
    public void onClick(View v) {
        ConfigEvent event =new ConfigEvent();
        switch (v.getId()){
            case R.id.act_btn_reload:
                //开始记录行程
                event.setStartCal(true);
                EventBus.getDefault().post(event);
                break;
            default:
                break;
        }
    }

    // 退出时间
    private long currentBackPressedTime = 0;

    // 退出间隔
    private static final int BACK_PRESSED_INTERVAL = 2000;
    @Override
    public void onBackPressed() {
        // 判断时间间隔

        if (System.currentTimeMillis()- currentBackPressedTime > BACK_PRESSED_INTERVAL) {

            currentBackPressedTime = System.currentTimeMillis();

            Toast.makeText(this, getString(R.string.toast_back_key), Toast.LENGTH_SHORT).show();

        } else {
            // 退出
            stopService(service);
            finish();
        }
    }
}
